package com.plugin.helloworld;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.io.FileOutputStream;

public class HelloWorldTask extends DefaultTask {
    private HelloWorldData helloMan;
    private File targetDirectory;
    private String fileName;

    @Nested
    public HelloWorldData getHelloMan(){
        return helloMan;
    }

    @OutputFile
    public File getTargetFile(){
        return new File(targetDirectory, fileName);
    }

    @Input
    public String getFileName(){
        return fileName;
    }

    @InputDirectory
    public File getTargetDirectory() {
        return targetDirectory;
    }

    @TaskAction
    public void writeObjectABC(){
        File targetFile = new File(targetDirectory, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(targetFile);
            byte[] bytes = helloMan.toString().getBytes();
            fos.write(bytes);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setHelloMan(HelloWorldData helloMan) {
        this.helloMan = helloMan;
    }

    public void setTargetDirectory(File targetDirectory) {
        this.targetDirectory = targetDirectory;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
